﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;

namespace UploadClient
{
    /// <summary>
    /// Handles the Exceptionmessages.
    /// </summary>
    public static class ExceptionHandling
    {
        //ToDo: secure it with lock()

        /// <summary>
        /// Displays the exception.
        /// </summary>
        /// <param name="ex"></param>
        public static void ThrowMessage(Exception ex)
        {
            ExceptionWindow exWindow;
            if (ex.GetType().Name.Equals("CustomKeyException"))
                exWindow = new ExceptionWindow((ex as CustomKeyException).ExceptionTitle,
                                                ex.Message,
                                                (ex as CustomKeyException).Image,
                                                ex,
                                                (ex as CustomKeyException).ExceptionTitle);
            else exWindow = new ExceptionWindow(ex);

            exWindow.Show();          
        }

        /// <summary>
        /// Returns recursiv the inner exceptions.
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        private static string GetInnerExceptionMessages(Exception ex)
        {
            if (ex.InnerException != null)
                return string.Format("{0}\n{1}", ex.InnerException.Message, GetInnerExceptionMessages(ex.InnerException));
            return string.Empty;
        }

    }

    /// <summary>
    /// CustomException with title and image for viewing in a MessageBox.
    /// The keys will be converted into the values from the RessourceDict.
    /// </summary>
    public class CustomKeyException : Exception
    {
        /// <summary>
        /// Default Exception.
        /// </summary>
        /// <param name="key">Key of the Key/Value-Pair from RessourceDic.</param>
        public CustomKeyException(string key)
            : base(LocalizationManager.ResourceManager.GetString(key))
        {
            this.ExceptionTitle = LocalizationManager.ResourceManager.GetString("ERR_0006");
            this.Image = SystemIcons.Error.ToBitmap();
        }

        /// <summary>
        /// Exception with inner exception.
        /// </summary>
        /// <param name="key">Key of the Key/Value-Pair from RessourceDic</param>
        /// <param name="innerException">Inner exception.</param>
        public CustomKeyException(string key, Exception innerException)
            : base(LocalizationManager.ResourceManager.GetString(key), innerException)
        {
            this.ExceptionTitle = LocalizationManager.ResourceManager.GetString("ERR_0006");
            this.Image = SystemIcons.Error.ToBitmap();
        }

        /// <summary>
        /// Exception with inner exception and values for the MessageBox.
        /// </summary>
        /// <param name="key">Key of the Key/Value-Pair from RessourceDic</param>
        /// <param name="exceptionTitle">MessageboxTitle.</param>
        /// <param name="image">MessageBoxImage.</param>
        public CustomKeyException(string key, [Optional, DefaultParameterValue("Exception")] string exceptionTitle, Bitmap image)
            : base(LocalizationManager.ResourceManager.GetString(key))
        {
            this.ExceptionTitle = exceptionTitle;
            this.Image = image;
        }

        /// <summary>
        /// Exception with inner exception and values for the MessageBox.
        /// </summary>
        /// <param name="key">Key of the Key/Value-Pair from RessourceDic</param>
        /// <param name="innerException">Inner exception.</param>
        /// <param name="exceptionTitle">MessageboxTitle.</param>
        /// <param name="image">MessageBoxImage.</param>
        public CustomKeyException(string key, Exception innerException, [Optional, DefaultParameterValue("Exception")] string exceptionTitle, Bitmap image)
            : base(LocalizationManager.ResourceManager.GetString(key), innerException)
        {
            this.ExceptionTitle = exceptionTitle;
            this.Image = image;
        }

        /// <summary>
        /// The MessageBox exception title.
        /// </summary>
        public string ExceptionTitle { get; private set; }
        
        /// <summary>
        /// The MessagboxImage.
        /// </summary>
        public Bitmap Image { get; private set; }
    }
}
